package com.niuwa.p2p.action.protocol;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.utils.DictUtil;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.entity.nwfile.NWFile;
import com.niuwa.p2p.entity.productTemplate.ProductProtocol;
import com.niuwa.p2p.entity.user.User;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.common.ICommonService;
import com.niuwa.p2p.service.protocol.IProtocolService;
import com.niuwa.p2p.vo.protocol.ProductProtocolVo;


public class ProtocolAction extends JsonAction {
	private static final long serialVersionUID = 6912556582397572041L;
	
	@Autowired
	private IProtocolService protocolService;
	
	@Autowired
	private ICommonService commonService;
	
	/** 产品协议信息表 */
	private ProductProtocol productProtocol;
	
	/** 协议id*/
	private Long protId;
	
	/** 协议文件ID*/
	private List<String> protFileID;
	
	/** 协议相关附件*/
	private List<NWFile> protAttachment;
	
	private String protType;//协议类型
	
	JsonResult filedErrorJsonResult;
	/**
	 * 跳转至协议列表页面
	 * @return
	 */
	@Action("gotoProtocolList")
	public String gotoProtocolList() {
		return "protocolList";
	}
	
	/**
	 * 获取协议列表信息
	 * @return
	 */
	@Action("protocolList")
	public String protocolList() {
		setObjectReSult(protocolService.selectProtocolBySelective(getParamMap()));
		return NONE;
	}
	
	/**
	 * 查看协议
	 * @return
	 */
	@Action("gotoProtocolDetails")
	public String gotoProtocolDetails() {
		protAttachment=protocolService.selectProtFilesByProtId(protId);
		return "protocolDetails";
	}
	
	/**
	 * 根据协议Id查看协议
	 * @return
	 */
	@Action("gotoProtocolById")
	public String gotoProtocolById() {
		setObjectReSult(protocolService.getProtocolByProtId(protId));
		return NONE;
	}
	
	/**
	 * 删除协议
	 * @return
	 */
	@Action("deleteProtocol")
	public String deleteProtocol() {
		User user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		setObjectReSult(protocolService.deleteProtocolBySelective(protId,user==null?null:user.getId()));
		return NONE;
	}


	@Action("gotoAddProtocolWindow")
	public String gotoAddProtocolWindow() {
		return "addProtocolWindow";
	}
	
 	/**
	 * 添加 产品协议信息 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(添加失败)
	 * @throws Exception 
	 */
	@Action("addProductProtocol")
	public String addProductProtocol() throws Exception {
		JsonResult result = new JsonResult();
		String errmsg = productProtocolFiledCheck();
		if (StringUtil.isEmpty(errmsg) == false) {
			result.setSuccess(false);
			result.setMsg(errmsg);
			this.setResult(result);
			return NONE;
		}
		protocolService.addProductProtocol(productProtocol, protFileID, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	
	/**
	 * 获取所有协议
	 * @return
	 */
	@Action("getAllProtocols")
	public String getAllProtocols() {
		//获取所有协议信息
		List<ProductProtocolVo> protocols = protocolService.selectAllProtocol();
		setObjectReSult(protocols);
		return NONE;
	}
	
	/**
	 * 根据协议名称获取版本号
	 * @return
	 */
	@Action("getProtocolByType")
	public String getProtocolByType() {
		List<ProductProtocolVo> protocolVersions = protocolService.getProtocolByType(protType);
		setObjectReSult(protocolVersions);
		return NONE;
	}
	
	public void setProtId(Long protId) {
		this.protId = protId;
	}

	public List<NWFile> getProtAttachment() {
		return protAttachment;
	}

	public ProductProtocol getProductProtocol() {
		return productProtocol;
	}

	public void setProductProtocol(ProductProtocol productProtocol) {
		this.productProtocol = productProtocol;
	}

	public List<String> getProtFileID() {
		return protFileID;
	}

	public void setProtFileID(List<String> protFileID) {
		this.protFileID = protFileID;
	}

	@Action("protNameAutocomplete")
	public String protNameAutocomplete() {
		JsonResult result = new  JsonResult();
		result.setSuccess(true);
		result.setData(protocolService.selectAllProtName());
		this.setResult(result);
		return NONE;
	}
	
 	/**
	 * 添加 产品协议信息check
	 * 
	 * @return check信息(无错误:"")
	 */
	private String productProtocolFiledCheck(){
		final String PROT_TYPE = "PROT_TYPE";
		if(StringUtil.isEmpty(productProtocol.getProtName())) {
			return this.getText("productProtocol.protName.RequiredString.message");
		} 
		
		if(productProtocol.getMainVersion() < 1) {
			return this.getText("productProtocol.mainVersion.minValue.message");
		}
		if(productProtocol.getMainVersion() > 1000) {
			return this.getText("productProtocol.mainVersion.maxValue.message");
		}
		
		if(productProtocol.getMinorVersion() < 0) {
			return this.getText("productProtocol.minorVersion.minValue.message");
		}
		if(productProtocol.getMinorVersion() > 1000) {
			return this.getText("productProtocol.minorVersion.maxValue.message");
		}
		
		String protType = productProtocol.getProtType();
		if(StringUtil.isEmpty(protType)) {
			return this.getText("productProtocol.protType.RequiredString.message");
		}
		if(StringUtil.isEmpty(DictUtil.getDictNameByTypeAndCode(PROT_TYPE, protType))) {
			return this.getText("productProtocol.protType.TypeError.message");
		}
		if(protFileID == null || protFileID.size() == 0){
			return this.getText("productProtocol.protFileID.RequiredString.message");
		}
		
		return "";
	}

	public String getProtType() {
		return protType;
	}

	public void setProtType(String protType) {
		this.protType = protType;
	}
	
}
